﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace ProjetoFase1
{
    public partial class FormManageActivities : Form
    {
        public DataSet serverDS, resiDS;
        SqlCeDataAdapter serverDA, resiDA;
        SqlCeCommandBuilder resiCB;
        SqlCeConnection cn;

        public FormManageActivities()
        {
            InitializeComponent();
            txtDate.Text = System.DateTime.Now.ToShortDateString();
        }

        private void FormManageActivities_Load(object sender, EventArgs e) 
        {

            cn = new SqlCeConnection("Data Source=\\My Documents\\" +
                    "g21.sdf;" +
                    "SSCE:Database Password=g21");
            SqlCeCommand cmd = cn.CreateCommand();
            cmd.CommandText = "select name,category,district from technician where id = '"+UserLogin.Username+"'";
            cn.Open();

            // reads the select and finds if there is an user
            SqlCeDataReader dr = cmd.ExecuteReader();
            dr.Read();

            try
            {
                UserLogin.Name = dr.GetString(0);
                UserLogin.AccessLevel = dr.GetInt16(1);
                UserLogin.District = dr.GetString(2);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }

            switch (UserLogin.AccessLevel)
            {
                case 0:
                    txtNivel.Text = "Trainee";
                    break;
                case 1:
                    txtNivel.Text = "Técnico";
                    break;
                case 2:
                    txtNivel.Text = "Supervisor";
                    break;
            }

            // loads data from servers
            //serverDA = new SqlCeDataAdapter
            //    ("select name, address from server", cn);
            //serverDS = new DataSet();
            //serverDA.Fill(serverDS, "server");

            // binds data to server combo
            //cmbServer.DataSource = serverDS.Tables["server"];
            //cmbServer.DisplayMember = "name";
            //cmbServer.ValueMember = "address";

            txtDistrict.Text = UserLogin.District;

            // closes the connection
            cn.Close();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            btnLoad.Enabled = false;

            // gets the residences info and bind the combo
            cn = new SqlCeConnection("Data Source=\\My Documents\\" +
                   "g21.sdf;" +
                    "SSCE:Database Password=g21");
            SqlCeCommand cmd = cn.CreateCommand();

            // loads data from residences
            resiDA = new SqlCeDataAdapter
                ("select r.* from residence as r, visiting_residence as v where r.resi_id=v.resi_id and "+
                    "v.id='"+UserLogin.Username+"' and datepart(d,v.visit_date)=datepart(d,getdate())", cn);
            resiCB = new SqlCeCommandBuilder(resiDA);
            resiDS = new DataSet();
            cn.Open();
            resiDA.Fill(resiDS, "residence");
            // bind combo box
            cmbMorada.DataSource = resiDS.Tables["residence"];
            cmbMorada.DisplayMember = "address";
            cmbMorada.ValueMember = "resi_id";

            cn.Close();

            btnLoad.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = false;
            // sends the updated data back to the server
            SyncDB.reinitializeSubscription(UserLogin.Username,UserLogin.Password);
            btnSave.Enabled = true;
        }

        private void menuLogout_Click(object sender, EventArgs e)
        {
            UserLogin.Logout();
            this.Close();
        }

        private void btnCriarCE_Click(object sender, EventArgs e)
        {
            try
            {
                // uses a select command to retrieve data
                SqlCeCommand cmd = cn.CreateCommand();
                cmd.CommandText = "select address,client,topology,cooling,heating,isolation,coverage,glasses from residence" +
                    " where resi_id=" + cmbMorada.SelectedValue;
                cn.Open();
                SqlCeDataReader dr = cmd.ExecuteReader();
                dr.Read();

                // create the data on EditCertificate class
                EditCertificate.Address = dr.GetString(0);
                EditCertificate.Client = dr.GetString(1);

                if (dr.IsDBNull(2))
                    EditCertificate.Topology = "";
                else
                    EditCertificate.Topology = dr.GetString(2);
                if (dr.IsDBNull(3))
                    EditCertificate.Cooling = 0;
                else
                    EditCertificate.Cooling = dr.GetInt16(3);
                if (dr.IsDBNull(4))
                    EditCertificate.Heating = 0;
                else
                    EditCertificate.Heating = dr.GetInt16(4);
                if (dr.IsDBNull(5))
                    EditCertificate.Isolation = 0;
                else
                    EditCertificate.Isolation = dr.GetInt16(5);
                if (dr.IsDBNull(6))
                    EditCertificate.Coverage = "";
                else
                    EditCertificate.Coverage = dr.GetString(6);
                if (dr.IsDBNull(7))
                    EditCertificate.Glasses = "";
                else
                    EditCertificate.Glasses = dr.GetString(7);
                
                EditCertificate.doneEdit = false;

                cn.Close();

                FormEditCertificate form = new FormEditCertificate();
                DialogResult foo = form.ShowDialog();
                if (foo == DialogResult.No)
                {
                    // do nothing
                }
                else if (foo == DialogResult.OK)
                {
                    // update the edited certificate
                    cn.Open();
                    cmd = cn.CreateCommand();
                    cmd.CommandText = "update residence set topology='" + EditCertificate.Topology +
                        "', cooling=" + EditCertificate.Cooling + ", heating=" +
                        EditCertificate.Heating + ", isolation=" + EditCertificate.Isolation +
                        ", coverage='" + EditCertificate.Coverage + "', glasses='" + EditCertificate.Glasses + "', " +
                        "category=" + UserLogin.AccessLevel + " where resi_id=" + cmbMorada.SelectedValue;
                    cmd.ExecuteNonQuery();
                    cn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }

    }
}